package com.androidemu;

import java.nio.Buffer;
import android.content.Context;
import android.graphics.Picture;
import android.os.Build.VERSION;
import android.view.SurfaceHolder;

import com.androidemu.Emulator;

public class Emulator {
	public static final int GAMEPAD_CIRCLE = (1 << 13);//
	public static final int GAMEPAD_CROSS = (1 << 14);//
	public static final int GAMEPAD_DOWN = (1 << 4);//
	public static final int GAMEPAD_TR = (1 << 11);//
	public static final int GAMEPAD_TR2 = (1 << 23);//
	public static final int GAMEPAD_TL = (1 << 10);//
	public static final int GAMEPAD_TL2 = (1 << 22);//
	// public static final int GAMEPAD_X = (1 << 6);
	// public static final int GAMEPAD_A = (1 << 7);
	public static final int GAMEPAD_RIGHT = (1 << 6);//
	public static final int GAMEPAD_LEFT = (1 << 2);//

	public static final int GAMEPAD_UP = 1;//
	public static final int GAMEPAD_START = (1 << 8);//
	public static final int GAMEPAD_SELECT = (1 << 9);//
	public static final int GAMEPAD_SQUARE = (1 << 12);//
	public static final int GAMEPAD_TRIANGLE = (1 << 15);//
	// public static final int GAMEPAD_Y = (1 << 14);
	// public static final int GAMEPAD_B = (1 << 15);
	// public static final int GAMEPAD_SUPERSCOPE_TURBO = (1 << 0);
	// public static final int GAMEPAD_SUPERSCOPE_PAUSE = (1 << 1);
	// public static final int GAMEPAD_SUPERSCOPE_CURSOR = (1 << 2);

	public static final int GAMEPAD_UP_LEFT = (GAMEPAD_UP | GAMEPAD_LEFT);//
	public static final int GAMEPAD_UP_RIGHT = (GAMEPAD_UP | GAMEPAD_RIGHT);//
	public static final int VIDEO_H = 240;
	public static final int VIDEO_W = 320;//
	public static final int GAMEPAD_DOWN_LEFT = (GAMEPAD_DOWN | GAMEPAD_LEFT);//
	public static final int GAMEPAD_DOWN_RIGHT = (GAMEPAD_DOWN | GAMEPAD_RIGHT);//

	private static Emulator emulator;//

	private native void cleanUp();

	public static Emulator createInstance(Context context, String dataDir) {
		if (emulator == null) {
			System.loadLibrary("psx");
			String libDir = new StringBuilder("/data/data/")
					.append(context.getPackageName()).append("/lib").toString();
			emulator = new Emulator();
			emulator.initialize(libDir, dataDir, Integer.parseInt(VERSION.SDK));
		}
		return emulator;
	}

	public static void destroyInstance() {
		emulator.cleanUp();
		emulator = null;
	}

	public static Emulator getInstance() {
		return emulator;
	}

	private native boolean initialize(String libDir, String dataDir, int sdk);

	public native void getScreenshot(Buffer buffer);

	public native boolean isROMLoaded();

	public native boolean loadBIOS(String file);

	public native boolean loadROM(String file);

	public native boolean loadState(String file);

	public native void pause();

	public native void power();

	public native void processTrackball(int key1, int duration1, int key2,
			int duration2);

	public native void reset();

	public native void resume();

	public native boolean saveState(String file);

	public native void setKeyStates(int states);

	public native void setNativeOption(String name, String value);

	public void setOption(String name, String value) {
		setNativeOption(name, value);
	}

	public native void setOverlay(Picture p);

	public native void setSurface(SurfaceHolder surface);

	public native void setSurfaceRegion(int x, int y, int w, int h);

	public native void unloadROM();
}